<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Events extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model(array('students_model', 'events_model', 'user_profiles_model', 'classes_model'));
        $this->load->helper(array('form'));
    }

    function index() {
        if(!in_array($this->tank_auth->get_role(), array('student', 'student_association', 'class_representative')))
            redirect('welcome');

        $class_id = $this->students_model->get_by_id($this->tank_auth->get_user_id())->class_id;
        $events = $this->events_model->get_for_class($class_id);

        foreach($events as $event)
            $data['events'][$event->id] = $this->_get_event_data($event);

        $this->ingesup->render('events/index', $data);
    }

    function admin() {
        if(!in_array($this->tank_auth->get_role(), array('teacher', 'student_association', 'class_representative')))
            redirect('welcome');

        $events = $this->events_model->get_by_user($this->tank_auth->get_user_id());

        foreach($events as $event)
            $data['events'][$event->id] = $this->_get_event_data($event);

        if($this->tank_auth->is_role('teacher')) {
            $classes = $this->classes_model->get_by_teacher($this->tank_auth->get_user_id());

            foreach($classes as $class) {
                $events = $this->events_model->get_by_user_for_class($this->tank_auth->get_user_id(), $class->id);
                $data['classes'][$class->id] = array();

                foreach($events as $event)
                    $data['classes'][$class->id][$event->id] = $this->_get_event_data($event);
            }
        } else {
            $class_id = $this->students_model->get_by_id($this->tank_auth->get_user_id())->class_id;
            $events = $this->events_model->get_by_user_for_class($this->tank_auth->get_user_id(), $class_id);
            $data['classes'][$class_id] = array();

            foreach($events as $event)
                $data['classes'][$class_id][$event->id] = $this->_get_event_data($event);
        }

        $this->ingesup->render('events/admin', $data);
    }

    function view($event_id) {
        $data['event'] = $this->_get_event_data($this->events_model->get_by_id($event_id));

        if(empty($data['event']))
            redirect('welcome');

        $this->ingesup->render('events/view', $data);
    }

    function edit($event_id=null) {
        if(isset($event_id))
            $data['event'] = $this->_get_event_data($this->events_model->get_by_id($event_id));
        else
            $data = '';
        $this->ingesup->render('events/edit', $data);
    }

    function delete($event_id) {
        $this->events_model->delete($event_id);
        $this->admin();
    }

    function link($class_id, $event_id) {
        $this->events_model->link($class_id, $event_id);
        $this->admin();
    }

    function unlink($class_id, $event_id) {
        $this->events_model->unlink($class_id, $event_id);
        $this->admin();
    }

    function show($class_id, $event_id) {
        $this->events_model->show($class_id, $event_id);
        $this->admin();
    }

    function mask($class_id, $event_id) {
        $this->events_model->mask($class_id, $event_id);
        $this->admin();
    }

    function _get_event_data($event) {
        $data = array(
            'id' => $event->id,
            'author_id' => $event->user_id,
            'author' => $this->user_profiles_model->get_user_fullname($event->user_id),
            'creation_date' => date('d/m/Y', strtotime($event->created)),
            'creation_time' => date('H:i:s', strtotime($event->created)),
            'title' => $event->title,
            'content' => $event->content
        );

        if(isset($event->last_update)) {
            $data['last_update_date'] = date('d/m/Y', strtotime($event->last_update));
            $data['last_update_time'] = date('H:i:s', strtotime($event->last_update));
            $data['updated'] = true;
        } else
            $data['updated'] = false;

        if(isset($event->published)) {
            $data['publish_date'] = date('d/m/Y', strtotime($event->published));
            $data['publish_time'] = date('H:i:s', strtotime($event->published));
            $data['published'] = true;
        } else
            $data['published'] = false;

        return $data;
    }
}